<?php
/**
*  景区退票
*/
class Order_edit_model extends CI_Model
{
	
	private $_table = 'order_edit';
    private $table_1 = 'order_base';

	/**
	 * 退单统计
	 */
	public function refundOrderTotal($params)
	{
		$sql = 'SELECT COUNT(order_edit.edit_id) AS `numrows` 
			    FROM (`order_base` AS order_base) 
			    INNER JOIN `order_edit` AS order_edit 
			    ON `order_base`.`order_id`=`order_edit`.`n_order_id`
			     WHERE `order_base`.`cat_id` IN (?) 
			     AND `order_base`.`payer_uid` =  ? ';

	    $result = $this->db->query($sql, array('1, 2, 4, 8, 16', $params['uid'])); 
	    return $result->row()->numrows;
	}

	/**
	 * 查询退单
	 */
	public function queryRefundOrder($params, $count, $start = 0)
	{
		$sql = 'SELECT order_base.order_id, order_base.goods_name, order_edit.amount_back, order_base.amount_fee, order_edit.edit_id, `order_edit`.`flag`, `order_edit`.`created_at`
			    FROM (`order_base` AS order_base) 
			    INNER JOIN `order_edit` AS order_edit 
			    ON `order_base`.`order_id`=`order_edit`.`n_order_id`
			    WHERE `order_base`.`cat_id` IN (?)
			    AND `order_base`.`payer_uid` =  ?
				ORDER BY `edit_id` DESC 
			    limit ' . $count . ',' . $start ;
			     
	    return $this->db->query($sql, array('1, 2, 4, 8, 16', $params['uid'])); 
	}

	/**
	 * 查询订单
	 */
	public function findById($editId)
	{
		$this->db->select('order_id, n_order_id, poundage, o_amount_fee, n_amount_fee, amount_back, flag, created_at, n_num, o_num, note, amount_back');
		$this->db->from($this->_table);
		$this->db->where('edit_id', $editId);
		$this->db->limit(1);
		return $this->db->get();
	}

	/**
	 * 插入
	 */
	public function insertInto($postData, $order)
    {
        $data = array(
            'order_id'     => $order->order_id,
            'n_order_id'   => 0,
            'other_id'     => $order->other_id,
            'out_other_id' => $order->out_other_id,
            'api_id'       => $order->api_id,
            'o_sfz'        => $order->sfz,
            'sfz'          => $postData['sfz'],
            'payer_uid'    => $order->payer_uid,
            'payer_name'   => $order->payer_name,
            'goods_id'     => $order->goods_id,
            'goods_name'   => $order->goods_name,
            'phone'        => $order->phone,
            'n_num'        => $postData['n_num'],
            'o_num'        => $order->goods_num,
            'o_amount_fee' => $order->amount_fee,
            'n_amount_fee' => $postData['n_amount_fee'],
            'poundage'     => $postData['poundage'],
            'amount_back'  => $postData['amount_back'],
            'o_plantime'   => $order->plantime,
            'plantime'     => $postData['plantime'],
            'created_at'   => date('Y-m-d H:i:s'),
            'flag'         => 1
        );
        $this->db->insert($this->_table, $data);
        return $this->db->insert_id();
    }

    /**
     * 修改订单
     */
    public function orderRefund($orderId)
    {
        $this->db->set('bit_opt', 'bit_opt | 128',false);
        $this->db->where('order_id',$orderId);
        return $this->db->update($this->table);
    }

    /**
     * 根据景区id查找订单
     */
    public function findByNewOrderId($NewOrderId)
    {
        $this->db->select('edit_id,order_id');
        $this->db->from($this->_table);
        $this->db->where('n_order_id', $NewOrderId);
        $this->db->limit(1);
        return $this->db->get();
    }

      /**
     * 联合查询order_edit和order_base表的信息
     */
    public function select_order_edit_base_info($where,$select,$limit = NULL)
    {
        $this->db->select($select);
        $this->db->from($this->table_1.' as ob');
        $this->db->join($this->_table.' as oe','ob.order_id = oe.order_id','inner');
        if(isset($where['order_id']) && $where['order_id']) {
            $this->db->where('ob.order_id',$where['order_id']);
        }
        if(isset($where['edit_id']) && $where['edit_id']) {
            $this->db->where('oe.edit_id',$where['edit_id']);
        }
        if($limit != NULL) {
            $this->db->limit($limit);
        }
    
        return $this->db->get();
    }

    public function update($postData)
    {
        $this->db->where('edit_id', $postData['edit_id']);
        if (!empty($postData['order_id'])) {
            $this->db->where('order_id', $postData['order_id']);
        }
        return $this->db->update($this->_table, $postData);
    }
}